<?php
/**
 * Created by PhpStorm.
 * User: jurentech
 * Date: 2019-06-17
 * Time: 22:35
 */
namespace app\master\controller;

class Xinxi extends Base
{
    public function index()
    {
        $count['all'] = db('order')->where(['status'=>1])->count();
        $beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));
        $endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;


        $count['day'] = db('order')->where(['status'=>1,'create_time'=>['between',[$beginToday,$endToday]]])->count();
        $this->assign('shuliang',$count);
        return $this->fetch();
    }

    public function chuli()
    {
        return $this->fetch();
    }

    public function reordertypes()
    {
        $id=input('id');
        $order_info=db('order')->where(['order_id'=>$id])->find();
        $son_info=db('son')->where(['son_id'=>$order_info['son_id']])->find();
        if($son_info['type']==1){
//            平台的
            db('reorder')->where(['order_id'=>$id])->update(['status'=>2,'end_time'=>time()]);
            return $this->_ajax_return(200,'成功');
        }else{
            $son_info['fee']=$son_info['fee']+50;
            $son_info['order_count']=$son_info['order_count']-1;
            $feemove['son_id']=$son_info['son_id'];
            $feemove['order_id']=$id;
            $feemove['type']=1;
            $feemove['fee']=50;
            $feemove['create_time']=time();
            $feemove['status']='申诉订单通过';
            db('fee_move')->insert($feemove);
            db('son')->where(['son_id'=>$son_info['son_id']])->update($son_info);
            db('reorder')->where(['order_id'=>$id])->update(['status'=>2,'end_time'=>time()]);
            return $this->_ajax_return(200,'成功');

        }
    }

    public function reordertype()
    {
        $id=input('id');
        db('order')->where(['order_id'=>$id])->update(['status'=>1]);
        db('reorder')->where(['order_id'=>$id])->update(['status'=>3,'end_time'=>time()]);
        return $this->_ajax_return(200,'成功');
    }

    public function getreorder()
    {
        $page['page'] = input("page");
        $page['limit'] = input("limit");
        $result['data'] = db('reorder')
            ->alias("r")
            ->field("o.order_id,r.create_time,r.status,o.name,o.phone,s.son_name,r.content")
            ->join("order o", "r.order_id = o.order_id")
            ->join("son s", "o.son_id = s.son_id")
            ->order('r.create_time DESC')
            ->page($page['page'], $page['limit'])
            ->select();
        foreach ($result['data'] as $v=>$k){
            $result['data'][$v]['create_time'] = date('Y-m-d H:i:s', $result['data'][$v]['create_time']);
        }
        $result['count'] = db('reorder')->alias("r")->join("order o", "r.order_id = o.order_id")->count();
        return $this->_table_return(0, "", $result['count'], $result['data']);
    }

    public function getorder()
    {
        $id=input('id');
        $page['page'] = input("page");
        $page['limit'] = input("limit");
        $result['data'] = db('order')
            ->order('create_time DESC')
            ->where(['status'=>1])
            ->page($page['page'], $page['limit'])
            ->select();
        foreach ($result['data'] as $v=>$k){
            $result['data'][$v]['area_name']=db('area')->where(['area_id'=>$result['data'][$v]['area_id']])->value('area_name');
            $result['data'][$v]['create_time'] = date('Y-m-d H:i:s', $result['data'][$v]['create_time']);
            $result['data'][$v]['son_name']=db('son')->where(['son_id'=>$result['data'][$v]['son_id']])->value('son_name');

        }
        $result['count'] = db('order')->where(['status'=>1])->count();
        return $this->_table_return(0, "", $result['count'], $result['data']);
    }

    public function change()
    {
        if(request()->isPost()){
            $order_id=input('post.order_id');
            $son_id=input('post.son_id');
            $order=db('order')->where(['order_id'=>$order_id])->find();
            $order['son_id']=$son_id;
            $order['create_time']=time();
            unset($order['order_id']);
            $order_id2=db('order')->insertGetId($order);
            $feemove['son_id']=$son_id;
            $feemove['order_id']=$order_id2;
            $feemove['type']=2;
            $feemove['fee']=50;
            $feemove['create_time']=time();
            $feemove['status']='手动分配订单扣除';
            db('fee_move')->insert($feemove);
            $count=db('son')->where(['son_id'=>$son_id])->value('order_count');
            $count=$count+1;
            db('son')->where(['son_id'=>$son_id])->update(['order_count'=>$count]);
            $this->_ajax_return(200,'成功','index');

        }
        $id=input('id');
        $order=db('order')->where(['order_id'=>$id])->find();
        $list=db('son')->where(['area_id'=>$order['area_id']])->select();
        $this->assign('order',$order);
        $this->assign('list',$list);
        return $this->fetch();
    }

    public function hdelete()
    {
        $id = input('id');
        $order=db('order')->where(['order_id'=>$id])->find();
        if($order['son_id']!=0){
            $count=db('son')->where(['son_id'=>$order['son_id']])->value('order_count');
            $count=$count-1;
            db('son')->where(['son_id'=>$order['son_id']])->update(['order_count'=>$count]);
        }
        db('order')->where(['order_id' => $id])->update(['status' => 2]);
        return $this->_ajax_return(200, '成功');
    }
}